package sma.guis;

import sma.agents.*;
import sma.infos.*;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Vector;
import javax.swing.*;

import org.freixas.jcalendar.JCalendarCombo;

//import com.toedter.calendar.JCalendar;

public class EmployeGui extends JFrame{

	/** Niveau etude **/
	/** 1 : DEUG (bac+2)
	/** 2 : Licence (bac+3)
	/** 3 : Maitrise (bac+4)
	/**  - 1er annee de master
	/** 4 : Master (bac+5)
	/**  - Master professionnel
	/**  - Master recherche
	/**  - Master metiers de l'enseignement
	/** 5 : Doctorat (bac+8) 
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Employe employe;
	
	/*****************************************************/
	/** LES CHAMPS AFFICHES SUR L'ECRAN                 **/
	/*****************************************************/
	
	/**Info personnel**/
	private JTextField 	sNomField,
						sPreNomField,
						
						sLieuNaissanceField,
						iAgeField,
						sEcoleLycenciePlusRecentField,
						iAnneeLycencieePlusRecentField;
	private JRadioButton bSexuelMaleField,
						bSexuelFemaleField;
	private ButtonGroup bSexuelGroup;
	
	private JComboBox iNiveauEtudeField;
	
	JCalendarCombo calDateNaissanceField;
	
	/**Info de travail**/
	private JComboBox iBrancheField;
	private JTextField 	fAnneesExperiencesField,
						sLieuTravailFeild,
						fSalaireSouhaiteField,
						aLanguagesEtrangeresField1,
						aLanguagesEtrangeresField2,
						aLanguagesEtrangeresField3
						;
	/*****************************************************/
	
	public EmployeGui(Employe e){
		super(e.getLocalName());
		
		employe = e;
		
		JPanel p = new JPanel();
		
		p.setLayout(new GridLayout(18,2));
		
		p.add(new JLabel("Information Personnelle : "));
		p.add(new JLabel(" "));
		
		p.add(new JLabel("    * Nom :"));
		this.sNomField = new JTextField(20);
		p.add(this.sNomField);
		
		p.add(new JLabel("    * Pre Nom :"));
		this.sPreNomField = new JTextField(20);
		p.add(this.sPreNomField);
		
		this.bSexuelMaleField = new JRadioButton("Male");
		this.bSexuelFemaleField = new JRadioButton("Female");
		this.bSexuelGroup = new ButtonGroup();
		this.bSexuelGroup.add(this.bSexuelMaleField);
		this.bSexuelGroup.add(this.bSexuelFemaleField);
		this.bSexuelMaleField.setSelected(true);
		p.add(this.bSexuelMaleField);
		p.add(this.bSexuelFemaleField);
		
		p.add(new JLabel("    * Date de naissance :"));
		this.calDateNaissanceField = new JCalendarCombo();
		p.add(this.calDateNaissanceField);
		
		p.add(new JLabel("    * Lieu naissance :"));
		this.sLieuNaissanceField = new JTextField(20);
		p.add(this.sLieuNaissanceField);
		
		p.add(new JLabel("    * Age :"));
		this.iAgeField = new JTextField(20);
		p.add(this.iAgeField);
		
		p.add(new JLabel("    * Niveau Etude :"));
		this.iNiveauEtudeField = new JComboBox();
		this.iNiveauEtudeField.addItem("1 : DEUG (bac+2)");
		this.iNiveauEtudeField.addItem("2 : Licence (bac+3)");
		this.iNiveauEtudeField.addItem("3 : Maitrise (bac+4)");
		this.iNiveauEtudeField.addItem("4 : Master (bac+5)");
		this.iNiveauEtudeField.addItem("5 : Doctorat (bac+8)");
		p.add(this.iNiveauEtudeField);
		
		p.add(new JLabel("    * Ecole plus recent :"));
		this.sEcoleLycenciePlusRecentField = new JTextField(20);
		p.add(this.sEcoleLycenciePlusRecentField);
		
		p.add(new JLabel("    * Annee lycencie :"));
		this.iAnneeLycencieePlusRecentField = new JTextField(20);
		p.add(this.iAnneeLycencieePlusRecentField);
		
		p.add(new JLabel("Information de travail :"));
		p.add(new JLabel(" "));
		
		p.add(new JLabel("Annees experiences :"));
		this.fAnneesExperiencesField = new JTextField(20);
		p.add(this.fAnneesExperiencesField);
		
		p.add(new JLabel("    * Branche :"));
		this.iBrancheField = new JComboBox();
		this.iBrancheField.addItem("1 : Informatique");
		this.iBrancheField.addItem("2 : Construction");
		this.iBrancheField.addItem("3 : Communication");
		this.iBrancheField.addItem("4 : Services Financiers");
		this.iBrancheField.addItem("5 : Tourisme ou Hotel");
		this.iBrancheField.addItem("6 : Production");
		p.add(this.iBrancheField);
		
		p.add(new JLabel("    * Lieu de travail : "));
		this.sLieuTravailFeild = new JTextField(20);
		p.add(this.sLieuTravailFeild);
		
		p.add(new JLabel("    * Salaire souhaite :"));
		this.fSalaireSouhaiteField = new JTextField(20);
		p.add(this.fSalaireSouhaiteField);
		
		p.add(new JLabel("    * Langages etrangeres : "));
		this.aLanguagesEtrangeresField1 = new JTextField(20);
		this.aLanguagesEtrangeresField2 = new JTextField(20);
		this.aLanguagesEtrangeresField3 = new JTextField(20);
		p.add(this.aLanguagesEtrangeresField1);
		p.add(this.aLanguagesEtrangeresField2);
		p.add(this.aLanguagesEtrangeresField3);
			
		JButton addButton = new JButton("Disposer Candidature");
		
		addButton.addActionListener(new ActionListener() {	
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try{
					InfoPersonnelles infoPer = new InfoPersonnelles();
					
					infoPer.setNom(sNomField.getText().trim());
					System.out.println("recup ecran : " + infoPer.getNom());
					infoPer.setPreNom(sPreNomField.getText().trim());
					
					Calendar cal = calDateNaissanceField.getCalendar();
					String day = cal.get(Calendar.DATE) + "/" + (cal.get(Calendar.MONTH) + 1) + "/" + cal.get(Calendar.YEAR);
					infoPer.setDateNaissance(day);
					
					infoPer.setLieuNaissance(sLieuNaissanceField.getText().trim());
					
					infoPer.setAge(Integer.parseInt(iAgeField.getText().trim()));
					
					infoPer.setEcoleLycenciePlusRecent(sEcoleLycenciePlusRecentField.getText().trim());
					
					infoPer.setAnneeLycencieePlusRecent(Integer.parseInt(iAnneeLycencieePlusRecentField.getText().trim()));
					
					infoPer.setSexuel(false);
					if(bSexuelMaleField.isSelected())
						infoPer.setSexuel(true);
					
					infoPer.setNiveauEtude(iNiveauEtudeField.getSelectedIndex());
					
					System.out.println("EmployeGui");
					InfoCV info = new InfoCV(infoPer);
					
					info.setAnneeExperiences(Float.parseFloat(fAnneesExperiencesField.getText().trim()));
					
					info.setBranche(iBrancheField.getSelectedIndex());
					
					info.setNomBranche();
					
					info.setLieuTravail(sLieuTravailFeild.getText().trim());
					
					Vector<String> languages = new Vector<String>(3);
					if (aLanguagesEtrangeresField1.getText().trim() != "")
						languages.add(aLanguagesEtrangeresField1.getText().trim());
					if (aLanguagesEtrangeresField2.getText().trim() != "")
						languages.add(aLanguagesEtrangeresField2.getText().trim());
					if (aLanguagesEtrangeresField3.getText().trim() != "")
						languages.add(aLanguagesEtrangeresField3.getText().trim());
					
					info.setLanguagesEtrangeres(languages);
					
					info.setSalaireSouhaite(Float.parseFloat(fSalaireSouhaiteField.getText().trim()));
					
					employe.insererCandidature(info);
					
				}catch(Exception err){
					JOptionPane.showMessageDialog(EmployeGui.this, "Invalid values. " + err.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		
		p.add(addButton);
		
		getContentPane().add(p, BorderLayout.SOUTH);
		
		setResizable(false);
	}
	
	public void showGui(){
		this.pack();
		Dimension sizeScreen = Toolkit.getDefaultToolkit().getScreenSize();
		int centerX = (int)sizeScreen.getWidth() / 2;
		int centerY = (int)sizeScreen.getHeight() / 2;
		setLocation(centerX - getWidth() / 2, centerY - getHeight() / 2);
		super.setVisible(true);
	}
}
